package com.richman.isn;

import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.richman.controller.DecreseRatio;
import com.richman.ps38.PS38Http;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.ISNEvent;
import com.richman.ps38.entity.ISNTeam;
import com.richman.utils.Common;

public class ISNManager {
	
	public static Vector<ISNTeam> ISNTeams = new Vector<ISNTeam>();
	
	public static Vector<ISNEvent> ISNEvents = new Vector<ISNEvent>();
	
	public static String isnUpdateStr = "";
	//public static ReadWriteLock HGEventsLock = new ReentrantReadWriteLock();
	
	public Vector<ISNHttp> accounts = new Vector<ISNHttp>();
	
	
	public void init() {
		Vector<String[]> tmps = DecreseRatio.mainWnd.getISNAccounts();
		
		for(int i = 0; i <tmps.size(); i++ ) {
			ISNHttp isnhttp = new ISNHttp();
			isnhttp.setLoginParams(tmps.elementAt(i)[0], tmps.elementAt(i)[1],
					tmps.elementAt(i)[2]);
			accounts.add(isnhttp);
		}
	}
	
	
	public void login() {
		
		try {
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if((accounts.elementAt(i).getStatusCode() == -1)&& (System.currentTimeMillis() > accounts.elementAt(i).getCanLoginTime())) {
					accounts.elementAt(i).login();
					//Thread.sleep(300);
				}
				
//				if((accounts.elementAt(i).getStatusCode() == -2) && (System.currentTimeMillis() > accounts.elementAt(i).getCanLoginTime())) {
//					accounts.elementAt(i).login();
//					Thread.sleep(300);
//				}

			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	
	public void checkIsOnline() {
		try {
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if( (accounts.elementAt(i).getStatusCode() == 1||accounts.elementAt(i).getStatusCode() == 2) && System.currentTimeMillis() > accounts.elementAt(i).getCanGetBalanceTime()) {
					accounts.elementAt(i).getbalance();
					//Thread.sleep(300);
				}
				
//				if((accounts.elementAt(i).getStatusCode() == -2) && (System.currentTimeMillis() > accounts.elementAt(i).getCanLoginTime())) {
//					accounts.elementAt(i).login();
//					Thread.sleep(300);
//				}

			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void grabEvents() {
		
		
		
		try {
			
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
			
			String showText = "";
			
			String successText = "";
			String failText = "";
			String notUsefulText = "";
			
			int second = DecreseRatio.mainWnd.getUpdateSeconds();
			
			
			
			int usefulAccountNum = 0;
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if(accounts.elementAt(i).getStatusCode() == 1 || accounts.elementAt(i).getStatusCode() == 2 || accounts.elementAt(i).getStatusCode() == 3) {
					usefulAccountNum++;
				}
			}
			
			
			
			if(usefulAccountNum*second < 3000 && usefulAccountNum != 0) {
				second = 3000/usefulAccountNum;
			}
			
			int millis = second;
			

			String failstr = " 登录中";
			if(System.currentTimeMillis() - DecreseRatio.startEngineTime > 50*1000) {
				failstr = " ISN无法获取赛事列表";
			}
			
			
			
			for(int i = 0; i <accounts.size(); i++ ) {

				ISNManager.ISNTeams.clear();
				
				ISNEvents.clear();
				
				boolean res1 = false;
				boolean res2 = false;
				
				if(accounts.elementAt(i).getStatusCode() == 1 || accounts.elementAt(i).getStatusCode() == 2 || accounts.elementAt(i).getStatusCode() == 3) {
					
					//Common.logOut("StatusCode:" + accounts.elementAt(i).getStatusCode());
					
					res1 = accounts.elementAt(i).getInplaybet();
					res2 = accounts.elementAt(i).getTotalBet();
					
					if(res1 && res2) {
						//Common.logOut("ISN获取赛事成功");
						DecreseRatio.mainWnd.updateTeamMatchISNTeams(ISNManager.ISNTeams);
					}
					
				}
				
				if(res1&&res2) {
					successText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}else{
					failText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}
				
				if(!successText.equals("")) {
					showText =  successText + " 获取ISN赛事列表成功" + "\n";
				}
				
				if(!failText.equals("")) {
					showText += failText + failstr + "\n";
				}
				isnUpdateStr = showText;
				
				DecreseRatio.mainWnd.updateHGTextArea(showText);
				DecreseRatio.mainWnd.updateISNAccountStatus(accounts.elementAt(i).getAccount(), accounts.elementAt(i).getStatusCode());
				
				DecreseRatio.isnps38EventManager.updateisnevents(ISNEvents);
				
				DecreseRatio.mainWnd.updateISNEventsTable(ISNEvents);

				Thread.sleep(500);

			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	

	
}
